package com.ueboot.weishop.admin.controller.role;

import com.alibaba.fastjson.JSON;
import com.ueboot.core.http.response.Response;
import com.ueboot.weishop.admin.controller.role.vo.ResourcesReq;
import com.ueboot.weishop.admin.controller.role.vo.ResourcesResp;
import com.ueboot.weishop.entity.role.Resources;
import com.ueboot.weishop.service.role.ResourcesService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Neel on 2017/9/18.
 */
@Slf4j
@RestController
@RequestMapping(value = "/role/resources")
public class ResourcesController {

    @Autowired
    private ResourcesService resourcesService;

    @RequiresPermissions("role:resources")
    @RequestMapping(value = "/page", method = RequestMethod.POST)
    public Response<Page<ResourcesResp>> page(@PageableDefault(value = 15, sort = { "id" }, direction = Sort.Direction.DESC) Pageable pageable, @RequestBody(required = false) ResourcesReq req) {
        log.info("/resources/page/");

        Page<Resources> entities = resourcesService.findBy(pageable);

        Page<ResourcesResp> body = entities.map(entity -> {
            ResourcesResp resp = new ResourcesResp();
            BeanUtils.copyProperties(entity, resp);

            return resp;
        });

        log.info("response: {}", JSON.toJSON(body));
        return new Response<>(body);
    }


    @RequiresPermissions("role:resources")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public Response<Void> save(@RequestBody ResourcesReq req) {
        log.info("/resources/save/");
        Resources entity = null;
        if (req.getId() == null) {
            entity = new Resources();
        } else {
            entity = resourcesService.get(req.getId());
        }
        BeanUtils.copyProperties(req, entity);

        resourcesService.save(entity);

        return new Response<>();
    }


    @RequiresPermissions("role:resources")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Response<Void> delete(Long[] id) {
        log.info("/resources/delete/{}", id);
        resourcesService.delete(id);
        return new Response<>();
    }

    @RequiresPermissions("role:resources")
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Response<Void> remove(Long id) {
        log.info("/resources/remove/{}", id);
        resourcesService.delete(id);
        return new Response<>();
    }


    @RequiresPermissions("role:resources")
    @RequestMapping(value = "/find", method = RequestMethod.POST)
    public Response<Void> find(@RequestBody ResourcesReq req) {
        log.info("/resources/find/");

        return new Response<>();
    }


}
